<?php

namespace app\index\controller;


use think\Controller;
use think\Db;
use app\common\model\VerifyCode;
use app\common\model\User;

class Register extends Controller
{
    public $user_model = null;
    public $user_attribute = null;
    public $user_real = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->user_model = new \app\common\model\User;
        $this->user_attribute = new \app\common\model\user\Attribute;
        $this->user_real = new \app\common\model\user\Real;
    }
    //注册页面
    public function index(){
        return $this->fetch("shop/register");
    }
    //无限极插入数据
    public function register_act(){

        $parent_id = input("parent_id");//父id
        //file_put_contents( "first_block.txt", "邀请parent_id5".$parent_id, FILE_APPEND);
        $name = input("name");//用户名
        $mobile = input("mobile");//电话号码
        $smscode = input("smscode");//验证码
        $password = input("password");//密码

//        if($mobile){
//            renderJson("正在维护!稍后再试",0);//维护系统稍后再试
//        }


        // 用户唯一性判断
        $model_user = $this->user_model->where('mobile', $mobile)->find();//如果手机号码，和role有1的就是表示注册过了
        if ($model_user){
            renderJson('该手机号码已被注册', 0);
        }
        // 验证码校验
        $auth_type = 'register_mobile';
        $code_obj = VerifyCode::checkCode($auth_type, $mobile, $smscode);
        $parent_name = "";
        if($parent_id){
            $user_info = Db::name("user")->where("uid",$parent_id)->field('uid,username')->find();
            if(!$user_info){
                $parent_id = 0;
                $parent_name = '';
            }else{
                $parent_id = $user_info["uid"];
                $parent_name = $user_info["username"];
            }
        }
        $register_data = [
            "username"=>$name,
            "mobile"=>$mobile,
            "password"=>$password,
            "referrer_id"=>$parent_id,
            "parent_name"=>$parent_name,
        ];

        $uid = $this->user_model->register_act($register_data);
        $userinfo = session("userinfo");

        if ($uid){
            if($userinfo["openid"]){
                Db::name("wx_users")->where("openid",$userinfo["openid"])->update(["uid"=>$uid]);
                $userinfo = Db::name("wx_users")->where("openid",$userinfo["openid"])->find();
                session("userinfo",$userinfo);
            }

            $code_obj->toSuccess(array('uid'=>$uid));
            model('User')->where('uid', $uid)->update([ 'last_login_time'=> time(), 'last_login_ip'=>request()->ip()]);
            $this->uid = $uid;
            $this->userInfo = model('User')->where('uid', $uid)->find();
            $this->user_attribute = Db::name("user_attribute")->where('uid', $uid)->find();
            renderJson('注册成功', 1);
        } else{
            renderJson('注册失败', 0);
        }



    }
    //第一次注册页面
    public function first(){
        return $this->fetch("shop/first");
    }
    //第二个页面
    public function apply(){
        return $this->fetch("shop/apply");
    }
}